import { deleteDepositApi, depositLogApi, getListApi, getPageStaticApi } from "@/api/waybillManagement/depositRegister";
import { ExclamationCircleOutlined } from "@ant-design/icons-vue";
import CommonHook from "@/hook/commonHook";
import { Modal, message } from "ant-design-vue";
import dayjs from "dayjs";
import { Ref, computed, createVNode } from "vue";
export default function OperationHook(state: {
  searchForm: DepositRegister.SearchForm;
  tableData: Ref<DepositRegister.ListRows[]>;
  tableRef: Ref<any>;
  dialogOpts: Base.DialogOpts;
  drawerOpts: Base.DrawerOpts;
  loading: Ref<boolean>;
  activeKey: Ref<string>;
  pageStat: Ref<Base.GeneraDataType>;
}) {
  const { searchForm, tableData, drawerOpts, loading, tableRef, dialogOpts, activeKey, pageStat } = state;
  const selectRows = computed<Array<DepositRegister.ListRows>>(() => {
    return tableRef.value.xTable.getCheckboxRecords() || [];
  });
  const { getNoZeroKey } = CommonHook();
  const getData = async () => {
    const params = {
      ...getParams()
    };
    loading.value = true;
    tableData.value = [];
    return getListApi(params)
      .then((res: any) => {
        const { code, data } = res;
        if (code === 200) {
          tableData.value = data.records || [];
          searchForm.beforePageNum = searchForm.pageNum;
          searchForm.total = Number(data.total);
        }
      })
      .finally(() => {
        loading.value = false;
      });
  };
  const getParams = () => {
    const { createByList, accountIdList, customerNames, customerIdList, proceedsTime, pageNum, pageSize, beforePageNum, companyIdList } = searchForm;
    const params: DepositRegister.SearchParams = {
      approvalState: Number(activeKey.value),
      accountIdList,
      createByList,
      proceedsStartTime: proceedsTime[0] ? dayjs(proceedsTime[0]).startOf("day").format("YYYY-MM-DD HH:mm:ss") : "",
      proceedsEndTime: proceedsTime[1] ? dayjs(proceedsTime[1]).endOf("day").format("YYYY-MM-DD HH:mm:ss") : "",
      pageNum,
      customerIdList,
      pageSize,
      beforePageNum,
      customerNames: [],
      companyIdList
    };
    if (customerNames) {
      params.customerNames = customerNames
        .trim()
        .replace(/,{1,}|，{1,}|\+{1,}|\s{1,}/g, ",")
        .split(",");
    }
    if (accountIdList.length) {
      params.accountIdList = accountIdList.map((t: any) => t[t.length - 1]);
    }
    if (createByList.length) {
      params.createByList = createByList.map((t: any) => t[t.length - 1]);
    }

    return params;
  };
  const pageStateic = () => {
    const params = getParams();
    return getPageStaticApi({ ...params }).then((res: any) => {
      const { code, data } = res;
      if (code === 200) {
        Object.assign(pageStat.value, {
          0: data[0],
          1: data[1]
        });
        activeKey.value = String(getNoZeroKey(pageStat.value, ["0", "1"]));
      }
    });
  };
  const searchHandle = async () => {
    searchForm.pageNum = 1;
    await pageStateic();
    getData();
  };
  const changeTabHandle = () => {
    getData();
  };
  // 新建来款单
  const addDepositHandle = () => {
    drawerOpts.title = `新建来款单`;
    drawerOpts.width = "1000px";
    drawerOpts.type = 1;
    drawerOpts.component = "AddDeposit";
    drawerOpts.visible = true;
  };
  // 删除来款
  const deleteDepositHandle = (row: DepositRegister.ListRows) => {
    Modal.confirm({
      title: "是否删除当前来款?",
      icon: createVNode(ExclamationCircleOutlined),
      onOk() {
        deleteDepositApi({ id: row.id }).then((res: any) => {
          const { code } = res;
          if (code === 200) {
            message.success("删除成功!");
            searchHandle();
          }
        });
      }
    });
  };
  // 查看删除记录
  const showDeleteLogHandle = () => {
    drawerOpts.title = `删除来款记录`;
    drawerOpts.width = "1200px";
    drawerOpts.type = 1;
    drawerOpts.component = "DeleteLog";
    drawerOpts.visible = true;
  };
  // 新建来款单
  const editDepositHandle = (row: DepositRegister.ListRows) => {
    drawerOpts.title = `编辑来款单`;
    drawerOpts.width = "1000px";
    drawerOpts.type = 2;
    drawerOpts.passData = row;
    drawerOpts.component = "AddDeposit";
    drawerOpts.visible = true;
  };
  // 查看来款单
  const showDepositHandle = (row: DepositRegister.ListRows) => {
    drawerOpts.title = `查看来款单`;
    drawerOpts.width = "1000px";
    drawerOpts.type = 3;
    drawerOpts.passData = row;
    drawerOpts.component = "AddDeposit";
    drawerOpts.visible = true;
  };
  // 查看日志
  const showLogHandle = (row: DepositRegister.ListRows) => {
    drawerOpts.title = `来款日志`;
    drawerOpts.width = "1200px";
    drawerOpts.type = 1;
    drawerOpts.component = "OperationLog";
    drawerOpts.passData = row;
    drawerOpts.visible = true;
  };
  // 获取日志
  const getLogFn = () => {
    return depositLogApi({ orderNum: drawerOpts.passData?.orderNum, businessId: drawerOpts.passData?.id });
  };
  // 批量导入来款
  const batchImportDepsoitHandle = () => {
    dialogOpts.title = `批量导入来款`;
    dialogOpts.width = "850px";
    dialogOpts.type = 1;
    dialogOpts.component = "BatchImportDeposit";
    dialogOpts.visible = true;
  };
  // 生成对账单
  const createBillHandle = () => {
    if (!selectRows.value.length) return message.error("请勾选至少一条费用！");
    dialogOpts.title = `生成对账单`;
    dialogOpts.width = "520px";
    dialogOpts.type = 1;
    dialogOpts.component = "CreateBill";
    dialogOpts.passData = selectRows.value;
    dialogOpts.visible = true;
  };

  return {
    searchHandle,
    getData,
    createBillHandle,
    changeTabHandle,
    addDepositHandle,
    deleteDepositHandle,
    showDeleteLogHandle,
    editDepositHandle,
    showLogHandle,
    getLogFn,
    showDepositHandle,
    batchImportDepsoitHandle
  };
}
